class Cover {
  constructor(domName) {
    this.domName = domName;
    this.domArr = [];
    this.domStyles = {};

    this.init();
  }

  init() {
    let domArr = [];
    let domCollection = document.body.getElementsByTagName("*");
    domCollection = Array.from(domCollection);
    domCollection.forEach((item) => {
      if (item.localName != "script") {
        domArr.push(item);
      }
    });

    this.domArr = domArr;

    this.domArr.forEach((item) => {
      this.domStyles[item.localName + "." + item.className.replace(/ /g, ".")] =
        {
          position: this.getStyle(item, "position"),
          top:
            this.getStyle(item, "top") == "auto"
              ? this.getStyle(item, "top")
              : parseInt(this.getStyle(item, "top")),
          left:
            this.getStyle(item, "left") == "auto"
              ? this.getStyle(item, "left")
              : parseInt(this.getStyle(item, "left")),
          bottom:
            this.getStyle(item, "bottom") == "auto"
              ? this.getStyle(item, "bottom")
              : parseInt(this.getStyle(item, "bottom")),
          right:
            this.getStyle(item, "right") == "auto"
              ? this.getStyle(item, "right")
              : parseInt(this.getStyle(item, "right")),
          width: parseInt(this.getStyle(item, "width")),
          height: parseInt(this.getStyle(item, "height")),
          zIndex:
            this.getStyle(item, "z-index") == "auto"
              ? this.getStyle(item, "z-index")
              : parseInt(this.getStyle(item, "z-index")),
          offsetTop: item.offsetTop,
          offsetLeft: item.offsetLeft,
        };
    });
  }

  isCover() {
    let res = false;
    let currentDomStyle = this.domStyles[this.domName];
    for (let item in this.domStyles) {
      let itemStyle = this.domStyles[item];
      if (this.domName == "" + item) {
        continue;
      } else {
        if (
          (itemStyle.position == "absolute" || itemStyle.position == "fixed") &&
          itemStyle.zIndex != "auto" &&
          itemStyle.zIndex > 0
        ) {
          if (
            currentDomStyle.zIndex != "auto" &&
            currentDomStyle.zIndex > itemStyle.zIndex
          ) {
            continue;
          } else {
            let location = {
              minX: currentDomStyle.offsetLeft,
              maxX: currentDomStyle.offsetLeft + currentDomStyle.width,
              minH: currentDomStyle.offsetTop,
              maxH: currentDomStyle.offsetTop + currentDomStyle.height,
            };
            if (
              //左上角
              (itemStyle.top >= location.minH &&
                itemStyle.top <= location.maxH &&
                itemStyle.left >= location.minX &&
                itemStyle.left <= location.maxX) ||
              //右上角
              (itemStyle.left + itemStyle.width >= location.minX &&
                itemStyle.left + itemStyle.width <= location.maxX &&
                itemStyle.top >= location.minH &&
                itemStyle.top <= location.maxH) ||
              // 左下角
              (itemStyle.top + itemStyle.height >= location.minH &&
                itemStyle.top + itemStyle.height <= location.maxH &&
                itemStyle.left >= location.minX &&
                itemStyle.left <= location.maxX) ||
              // 右下角
              (itemStyle.left + itemStyle.width >= location.minX &&
                itemStyle.left + itemStyle.width <= location.maxX &&
                itemStyle.top + itemStyle.height >= location.minH &&
                itemStyle.top + itemStyle.height <= location.maxH) ||
              // 覆盖元素 左边
              (itemStyle.left >= location.minX &&
                itemStyle.left <= location.maxX &&
                itemStyle.top <= location.minH &&
                itemStyle.top + itemStyle.height >= location.minH) ||
              // 覆盖元素 下边
              (itemStyle.left <= location.minX &&
                itemStyle.left + itemStyle.width >= location.maxX &&
                itemStyle.top + itemStyle.height >= location.minH &&
                itemStyle.top + itemStyle.height <= location.maxH) ||
              // 覆盖层 上边
              (itemStyle.left <= location.minX &&
                itemStyle.left + itemStyle.width >= location.maxX &&
                itemStyle.top >= location.minH &&
                itemStyle.top <= location.maxH) ||
              // 覆盖层 右边
              (itemStyle.top <= location.minH &&
                itemStyle.top + itemStyle.height >= location.maxH &&
                itemStyle.left + itemStyle.width >= location.minX &&
                itemStyle.left + itemStyle.width <= location.maxX) ||
              // 终极情况 完全覆盖
              (itemStyle.top <= location.minH &&
                itemStyle.left <= location.minX &&
                itemStyle.left + itemStyle.width >= location.maxX &&
                itemStyle.top + itemStyle.height >= location.maxH)
            ) {
              res = true;
            }
          }
        } else {
          continue;
        }
      }
    }
    return res;
  }

  getStyle(obj, attr) {
    return obj.currentStyle
      ? obj.currentStyle[attr]
      : getComputedStyle(obj)[attr];
  }
}
